package app.modules.infrastructure.lov.persistence.dao;

import app.modules.common.constant.TenantConstants;
import app.modules.infrastructure.lov.dto.web.WebValueItemDTO;
import app.modules.infrastructure.lov.dto.web.WebValueItemQueryDTO;
import app.modules.infrastructure.lov.persistence.entity.ValueItemEO;
import app.modules.infrastructure.lov.persistence.mapper.ValueItemMapper;
import app.modules.infrastructure.lov.util.conversion.ValueItemConvertUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jasmine.framework.common.util.CheckUtil;
import jasmine.framework.common.util.CollectionUtil;
import jasmine.framework.context.CurrentSubject;
import jasmine.framework.database.mybatisplus.dao.BaseI18nEntityDAO;
import jasmine.framework.database.mybatisplus.i18n.I18nEntityUtil;
import jasmine.framework.database.mybatisplus.wrapper.LambdaQueryWrapperWithI18n;
import org.springframework.stereotype.Repository;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author mh.z
 */
@Repository
public class ValueItemDAO extends BaseI18nEntityDAO<ValueItemMapper, ValueItemEO> {

    /**
     * 分页查询值项
     *
     * @param query
     * @param page
     * @return
     */
    public List<WebValueItemDTO> pageWebValueItemDTOsByCond(WebValueItemQueryDTO query, Page page) {
        CheckUtil.notNull(query, "query null");
        CheckUtil.notNull(page, "page null");
        LambdaQueryWrapperWithI18n<ValueItemEO> wrapper = LambdaQueryWrapperWithI18n.wrapper();

        Long valueSetId = query.getValueSetId();
        wrapper.eq(ValueItemEO::getValueSetId, valueSetId);
        wrapper.orderByAsc(ValueItemEO::getShowOrder);
        wrapper.orderByAsc(ValueItemEO::getValueCode);
        // 租户ID
        wrapper.eq(ValueItemEO::getTenantId, CurrentSubject.getTenantId());

        Page<ValueItemEO> pageResult = baseMapper.selectPageWithI18n(page, wrapper);
        List<ValueItemEO> recordList = pageResult.getRecords();
        List<WebValueItemDTO> returnList = ValueItemConvertUtil.valueItemEO2WebValueItemDTO(recordList);

        return returnList;
    }

    /**
     * 查询值项
     *
     * @param valueSetCode
     * @param valueCodes
     * @return
     */
    public List<ValueItemEO> listByValueSetGlobalCodeNoI18n(String valueSetCode, Collection<String> valueCodes) {
        if (CollectionUtil.isEmpty(valueCodes)) {
            return Collections.emptyList();
        }

        Collection<Long> tenantIds = Arrays.asList(TenantConstants.SYSTEM_TENANT_ID, CurrentSubject.getTenantId());
        return baseMapper.listByValueSetGlobalCodeNoI18n(valueSetCode, valueCodes, tenantIds);
    }

    /**
     * 查询值项
     *
     * @param valueSetCode
     * @param valueCodes
     * @return
     */
    public List<ValueItemEO> listByValueSetGlobalCode(String valueSetCode, Collection<String> valueCodes) {
        if (CollectionUtil.isEmpty(valueCodes)) {
            return Collections.emptyList();
        }

        Collection<Long> tenantIds = Arrays.asList(TenantConstants.SYSTEM_TENANT_ID, CurrentSubject.getTenantId());
        List<ValueItemEO> list = baseMapper.listByValueSetGlobalCodeNoI18n(valueSetCode, valueCodes, tenantIds);

        if (CollectionUtil.isNotEmpty(list)) {
            I18nEntityUtil.populateI18n(list);
        }

        return list;
    }

    /**
     * 根据值代码统计
     *
     * @param valueSetId
     * @param valueCode
     * @param excludeIds
     * @return
     */
    public long countByValueCode(Long valueSetId, String valueCode, @Nullable Collection<Long> excludeIds) {
        LambdaQueryWrapper<ValueItemEO> wrapper = Wrappers.lambdaQuery();

        wrapper.eq(ValueItemEO::getValueSetId, valueSetId);
        wrapper.eq(ValueItemEO::getValueCode, valueCode);
        wrapper.eq(ValueItemEO::getTenantId, CurrentSubject.getTenantId());
        wrapper.notIn(CollectionUtil.isNotEmpty(excludeIds), ValueItemEO::getId, excludeIds);

        return baseMapper.selectCount(wrapper);
    }

}
